<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/view/common/kendocommon.jsp"%>
<!DOCTYPE html>
<html>
<head>
    <title>主管分配客户销售顾问</title>
    <meta charset="utf-8">
    <style>
    	table{font-size:12px}
    </style>
    <script>
    var selectItem=new Object();
        $(document).ready(function() {
        	
        	$(".various").fancybox({
        		maxWidth	: 1000,
        		maxHeight	: 700,
        		fitToView	: false,
        		width		: '80%',
        		height		: '80%',
        		autoSize	: false,
        		closeClick	: false,
        		openEffect	: 'none',
        		closeEffect	: 'none'
        	});
        	
        	$("#toolbar").kendoToolBar({
	            resizable: false,
	            items: [
	            	{ type: "separator" },
	                //{ template: toolbarTemp},
					//{ type: "separator" },
	              	{ template: '<label>用户手机号:</label><input id="phone" class="k-textbox"/>'},
	                { template: '<label>用户名</label><input id="user_name" class="k-textbox" style="width:120px"/>'},
	                //{ template: '<label>孩子手机号:</label><input id="baby_phone" class="k-textbox"/>'},
	                { template: '<label>家庭称谓</label><input id="member_name" class="k-textbox" style="width:120px"/>'},
	                { type: "button", text: "查询",click: queryCustomerList },
	                { template: "<br/>" },
	                { template: "<label>门店分配</label><input name='customAssit' id='customAssit'>" },
	                { type: "separator" },
	                { type: "button", text: "门店分配",click: saveAssistCustomer }
	            ]
	        });

	        //客户列表数据源
            customerDS = new kendo.data.DataSource({
                transport: {
                    read:  {
                        url: "${path}/business/getCommonUserList",
                        dataType: "json",
                        data: function() {
					        return {
					        	phone:$("#phone").val(),
					        	user_name:$("#user_name").val(),
					        	member_name:$("#member_name").val()
					        };
					    }
                    }
                },
                //group: [{field: "businessTypeCode"}],
                serverPaging: true,
                serverFiltering: true,
                pageSize: 20,
                schema: {
                	data:"data",
               		total:"total"
                }
            });

            //查询客户信息
      		var queryCustomer = function(dataItem){
      			return "<a href='${path}/assist/memberInfo?c_user_id="+dataItem.c_user_id+"' style='color:#000' class='various' data-fancybox-type='iframe' title='会员信息'>" + dataItem.real_name + "</a>";
      		};
      		
      		var gridhight=document.body.clientHeight-38;
            
            //客户列表
            $("#customergrid").kendoGrid({
                dataSource: customerDS,
                height:gridhight,
                pageable: {
					refresh: true,
					pageSizes: true,
					buttonCount: 5,
					page: 1,
					pageSize: 20,
					pageSizes: [20, 30,40],
					messages: {
						display: "显示 {0}-{1} 共 {2} 项",
						empty: "没有数据",
						itemsPerPage: "每面显示数量",
						first: "第一页",
						last: "最后一页",
						next: "下一页",
						previous: "上一页"
					}
				},
                selectable: "multiple row",
                allowCopy: true,
                //editable: 'incell',
                columns: [
                	{ field:"checked", template: "<input type='checkbox' class='gridchk'/>",width: "120px",
                		headerTemplate: '<input type="checkbox" id="check-all"/>',editable: false},
                    { field:"real_name", title: "客户姓名",width: "250px",template:queryCustomer},
                    { field:"phone", title: "客户手机号",width: "150px",editable: false},
                    { field:"sex", title: "客户性别",width: "150px",editable: false},                   
                    { field:"member_name", title: "家庭称谓",width: "250px",editable: false},
                 //   { field:"baby_name", title: "宝宝名字",width: "150px",editable: false},                    
                    { field:"admission_note", title: "入会备注",width: "250px"},
                    { field:"assistantName", title: "销售顾问",width: "250px"},
                    { field:"note", title: "分配备注",width: "250px"}
                    //{ field: "UnitsInStock", title:"Units In Stock", width:"120px"},//,format:"{0:yyyy/MM/dd}"
                    //{ command: [{name:"editrow",text:"<span class='k-icon k-edit'></span>查看",click:queryCustomerInfo,buttonType:"ImageAndText"}], title: "操作", width: "100px" }
                ]
            });
            
            //销售顾问列表数据源
            var parkDataSource = new kendo.data.DataSource({
           // var CustomAssistDataSource = new kendo.data.DataSource({
	  			transport: {
	  			  read: {
	  			  //url: spera.spera_consume
	    		  url: "${path}/business/getParkList",
	     		  dataType: "json"
						
	   				 }
	 			 }
			});
            
            //门店分配
			$("#customAssit").kendoComboBox({
		     	dataValueField: "kids_park_id",
		        dataTextField: "name",
		        dataSource: parkDataSource,
		        select: function(e) {
		        	//debugger;
					var value = this.value();
    				var item = e.item;  							      					    
					selectItem=e.sender.dataItem(item);
					console.log('selectItem');
					console.log(selectItem);
				}
			});
            
            $('#check-all').change(function(){
            	var customergrid = $("#customergrid").data("kendoGrid");
            	$.each(customergrid.items(), function (i, item) {
					var cbxController = $(item).find(".gridchk");
					var isChecked = cbxController.is(":checked");
					if(isChecked){
						cbxController.prop("checked",false); 
					}else{
						cbxController.prop("checked",true); 
					}
				});
            });
            
		});
        
        
		//辅导员删除绑定关系
		var deleteAssistCus = function(e){
			var assistcustomergrid = $("#assistcustomergrid").data("kendoGrid");
			e.preventDefault();
            var data = assistcustomergrid.dataItem($(e.currentTarget).closest("tr"));
            console.log(data);
            if(data.cuserid){
            	$.ajax({
			        url: "${path}/assist/delAssistUserInfo",
			        dataType: 'json',
	   				//contentType: "application/json",
			        data: {c_user_id:data.cuserid},
			        type:"POST", 
			        success: function (text) {
			        	$("#assistcustomergrid").data("kendoGrid").dataSource.fetch();
			        	toastr.info(text.msg, "提示", toastrOptsBottom);
			        },
			        error: function (jqXHR, textStatus, errorThrown) {
			        	toastr.info("与远程服务器交互异常，请规范操作或重新登录", "提示", toastrOptsBottom);
			        }
			    });
            }
		}
		
		var queryCustomerList = function(e){//查询
			var customergrid = $("#customergrid").data("kendoGrid");
			$("#customergrid").data("kendoGrid").dataSource.fetch();
			$("#customergrid").data("kendoGrid").dataSource.page(1);
			
		//	var assistcustomergrid = $("#assistcustomergrid").data("kendoGrid");
		//	$("#assistcustomergrid").data("kendoGrid").dataSource.fetch();
		}
		
		//保存
		var saveAssistCustomer = function(e){
			
			//alert('addCustomAssist');
			
			if(selectItem=={}){
				toastr.info("请选择销售顾问", "提示", toastrOptsBottom);
			}
			
			var customergrid = $("#customergrid").data("kendoGrid");
			if(customergrid.items().length==0){
				return toastr.info("请先选择用户", "提示", toastrOptsBottom);
			}
			var checkedArr = new Array();
		//	checkedArr.push({cUserId:$('#park').val()});
		//	checkedArr.tUserID='王老五';
			//var model = {};
		//	var note=prompt("请输入必要的备注，可以为空","");
			$.each(customergrid.items(), function (i, item) {
				var cbxController = $(item).find(".gridchk");
				var isChecked = cbxController.is(":checked");
				var curData = customergrid.dataItem(item);
				//debugger;
				if (isChecked){
					//model.id = curData.id;
					//model.parkPrice = curData.parkPrice;
					checkedArr.push({c_user_id:curData.c_user_id});
				
					//curData.note = note;
					//checkedArr.push(curData);
				}
			});
			
			//alert(note);
			if(checkedArr.length==0){
				toastr.info("请选择客户", "提示", toastrOptsBottom);
				return false;
			}
			
			var UserList=JSON.stringify(checkedArr);

			$.ajax({
		        url: "${path}/business/updateUserPark",
		        dataType: 'json',
   			//	contentType: "application/json",
		        data: {"kids_park_id":selectItem.kids_park_id,"userList":UserList},
		        type:"POST", 
		        success: function (date) {
		        	if(date.status==0){
		        		toastr.info(date.msg, "异常", toastrOptsBottom);
		        	}else{
		        		toastr.info("添加成功", "提示", toastrOptsBottom);
		        		$("#customergrid").data("kendoGrid").dataSource.fetch();
		        	}
		        //	$("#assistcustomergrid").data("kendoGrid").dataSource.fetch();
		        //	toastr.info(text.msg, "提示", toastrOptsBottom);
		        },
		        error: function (jqXHR, textStatus, errorThrown) {
		        	toastr.info("与远程服务器交互异常，请规范操作或重新登录", "提示", toastrOptsBottom);
		        }
		    });
		}
		
		//用户注册
		var registerPop = function(e){
			$.fancybox($("#register"));
		}
		
    </script>
</head>
<body>
	<div id="toolbar" style="margin:2px"></div>
    <div id="customergrid" style="margin:2px;font-size:10px"></div>
    <div id="assistcustomergrid" style="margin:2px;font-size:10px"></div>
    
    <div style="display: none;">		
		<div id="inlineForm" style="width:700px;overflow:auto;">
			<table>
                <tr>
                    <td style="width:100px;padding:5px">
                        <span  style="width:90px">内容</span>
                    </td>
                    <td style="padding:5px">
                        <textarea name="note" id="note" style="width:495px;height:200px" class="k-textbox"></textarea>
                    </td>
                </tr>
            </table>
		</div>
	</div>
	
	<a href="${path}/assist/registerbyassist" class="fancybox fancybox.iframe" id="register" title="客户录入" style="display:none"></a>
</body>
</html>
